<?php
session_start();
?>
<?php
require "lib/db.php";
require "lib/case-function.php";
require "lib/customer-function.php";
require "lib/order-function.php";
require "lib/category-function.php";
require "lib/subcategory-function.php";
require "lib/product-function.php";
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Add Order</title>
<link rel="stylesheet" type="text/css" href="css/home.css">
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>

<script>
	$(document).ready(function() {
			$("#order").css({"background-color":"#FFF",	
	"color":"#000"});
	
	var intelligentSuggestion = "checked";
	
		$("#cbCategoryName").change(function(){
				var categoryID = $(this).val();
				$.get("list-subcategory-by-categoryid.php",{categoryID:categoryID},function(data){
					$("#cbSubCategoryName").html(data);	
				});	
				$.get("list-product-by-subcategoryid.php",{subCategoryID:$("#cbSubCategoryName").val()},function(data){
					$("#cbProductName").html(data);
				});	
				
				//set default product and number
				$("#cbProductName").val(0);
				$("#txtProductNumber").val(0);
				var productNumber = $("#txtProductNumber").val();//just missing # in #txtProductNumber make me crazy in a lot
				$.get("ordercontroller.php",{productID:0,productNumber:0},function(data){
					$("#orderinfo").html(data);
				});			
		});
		
		if($("#hfSuggestionSubCategoryID").val()!=0){
				$("#cbSubCategoryName").val($("#hfSuggestionSubCategoryID").val());
			}	
		
		$.get("list-subcategory-by-categoryid.php",{categoryID:$("#cbCategoryName").val()},function(data){
				$("#cbSubCategoryName").html(data);	
			});	 
		
		$("#cbSubCategoryName").change(function(){
				var subCategoryID = $(this).val();
				//alert(subCategoryID);
				$.get("list-product-by-subcategoryid.php",{subCategoryID:subCategoryID},function(data){
					$("#cbProductName").html(data);	
				});	
						
				//set default product and number
				$("#cbProductName").val(0);
				$("#txtProductNumber").val(0);
				var productNumber = $("#txtProductNumber").val();//just missing # in #txtProductNumber make me crazy in a lot
				$.get("ordercontroller.php",{productID:0,productNumber:0},function(data){
					$("#orderinfo").html(data);
				});	
		});
		
		$.get("list-product-by-subcategoryid.php",{subCategoryID:$("#cbSubCategoryName").val()},function(data){
				$("#cbProductName").html(data);
			});	
		
		//when choose product, the product number default is 1	
		$("#cbProductName").change(function(){
				var productID = $(this).val();
				//alert(productID);
				if(productID > 0){
					$("#txtProductNumber").val(1);
				}else{
					$("#txtProductNumber").val(0);
				}
				var productNumber = $("#txtProductNumber").val();//just missing # in #txtProductNumber make me crazy in a lot
				//alert(productNumber);
				$.get("ordercontroller.php",{productID:productID,productNumber:productNumber},function(data){
					$("#orderinfo").html(data);
				});
			
			});
			
		//when product number change
		$("#txtProductNumber").change(function(){
				var productID = $("#cbProductName").val();
				//alert(productID);
				if(productID == 0){
					$("#txtProductNumber").val(0);
				}
				var productNumber = $("#txtProductNumber").val();//just missing # in #txtProductNumber make me crazy in a lot
				//alert(productNumber);
				$.get("ordercontroller.php",{productID:productID,productNumber:productNumber},function(data){
					$("#orderinfo").html(data);
				});
			});
			
		var count = 0;
		$("#intelligent-suggestion").change(function(){
				count = count +1;
				if(count %2 != 0){
					$("#intelligent-suggestion").removeAttr("checked");
				}else{
					$("#intelligent-suggestion").attr("checked","checked");
				}
	
				/*if($("#intelligent-suggestion").attr("checked") != null){
				alert($("#intelligent-suggestion").attr("checked"));
				}else{
					alert("not check");
				}*/
		});
		
		//when click on add product button to add more product
		$("#btnAddProduct").click(function(){
			if($("#cbProductName").val()==0 || $("#txtProductNumber").val()==0){
				alert("Please chooose product and number of product!");
			}else{//choose product and number of product
				var productID = $("#cbProductName").val();
				var productNumber = $("#txtProductNumber").val();//just missing # in #txtProductNumber make me crazy in a lot
				
				
				if($("#intelligent-suggestion").attr("checked") == 'checked'){
					intelligentSuggestion = "checked";
					//alert(intelligentSuggestion);
				}else{
					intelligentSuggestion = "unchecked";
					//alert(intelligentSuggestion);
				}
				
				$.get("ordercontroller.php",{intelligentSuggestion:intelligentSuggestion,productID:productID,productNumber:productNumber,action:"addProduct"},function(data){
					$("#orderinfo").html(data);
					var suggestProductID = $("#hfIntelligentSuggestion").val();
					//alert("productid: "+suggestProductID);
					var suggestCategoryID = $("#hfSuggestionCategoryID").val();
					//alert("categoryid: "+suggestCategoryID);
					var suggestSubCategoryID = $("#hfSuggestionSubCategoryID").val();
					//alert("subcategoryid: "+suggestSubCategoryID);
					if(suggestProductID != 0){
						$("#cbCategoryName").val(suggestCategoryID);	
						$.get("list-subcategory-by-categoryid.php",{categoryID:suggestCategoryID},function(data){
						$("#cbSubCategoryName").html(data);	
						$("#cbSubCategoryName").val(suggestSubCategoryID);
					});	
						$.get("list-product-by-subcategoryid.php",{subCategoryID:suggestSubCategoryID},function(data){
						$("#cbProductName").html(data);
						$("#cbProductName").val(suggestProductID);	
						$("#txtProductNumber").val(1);	
					});		
						
					}else{
					//after add productid and product number into array, set default to some combobox to 0
						$("#cbCategoryName").val(0);
						$("#cbSubCategoryName").val(0);
						$("#cbProductName").val(0);
						$("#txtProductNumber").val(0);
					}
				});
			}
		});
		
		
    });
</script>


<style>
	#content{
		width:700px;
		margin:10px auto;
		opacity:0.8;
	}
	
	select, #txtProductNumber{
		width:400px;
	}
	
	#tdrequire{
		text-align:left;	
	}
	
	#btnAddProduct, #btnAddOrder{
		width:635px;
	}

</style>

</head>

<body>

<!--menu-->
<?php
require "menu.php";
?>

<?php

$order = array();
array_push($order,1,2);
$productNumberTotal = 0;
$orderAmountTotal = 0;


?>

<!--content-->
<div id="content">
<form action="ordercontroller.php" method="post">
<table width="100%" border="0" cellspacing="5">
 <tr>
    <td><input type="checkbox" id="intelligent-suggestion" checked="checked"> Intelligent Suggestion</td>
    <td id="tdrequire"><span id="require">*</span>: required value</td>
  </tr>
  <tr>
    <td>Customer Name<span id="require">*</span></td>
    <td>
    	<select name="cbCustomer">
       		<?php
			$listcustomer = getAllCustomer();
			while($customer = mysql_fetch_array($listcustomer)){
			?>
        	<option value="<?php echo $customer['CustomerID']; ?>"><?php echo $customer['CustomerName']; ?></option>
            <?php
			}
			?>
        </select>
    </td>
    </tr>
    <tr>
    <td>Order Year<span id="require">*</span></td>
    <td>
    	<select name="cbOrderYear">
        	<option value="2013">2013</option>
            <option value="2014">2014</option>
        </select>
    </td>
    </tr>
    <tr>
    <td>Order Month<span id="require">*</span></td>
    <td>
    	<select name="cbOrderMonth">
        	<option value="1">January</option>
            <option value="2">February</option>
            <option value="3">March</option>
            <option value="4">April</option>
            <option value="5">May</option>
            <option value="6">June</option>
            <option value="7">July</option>
            <option value="8">August</option>
            <option value="9">September</option>
            <option value="10">October</option>
            <option value="11">November</option>
            <option value="12">December</option>
        </select>
    </td>
  </tr>
  <tr>
  	<td>Category Name<span id="require">*</span></td>
    <td>
        <select name="cbCategoryName" id="cbCategoryName">
        	<?php
			$listCategory = getAllCategory();
			echo '<option value="0">--Choose--</option>';
			if(mysql_num_rows($listCategory)==0){
				echo '<option value="0">Empty</option>';
			}else{
			while($category = mysql_fetch_array($listCategory)){
			?>
        	<option value="<?php echo $category['CategoryID'] ?>"><?php echo $category['CategoryName'] ?></option>
            <?php
				}
			}
			?>
        </select>
    </td>
    </tr>
    <tr>
    <td>SubCategory Name<span id="require">*</span></td>
    <td>
        <select name="cbSubCategoryName" id="cbSubCategoryName">
        	<?php
			$listSubCategory = getAllSubCategory();
			echo '<option value="0">--Choose--</option>';
			if(mysql_num_rows($listSubCategory)==0){
				echo '<option value="0">Empty</option>';
			}else{
			while($subCategory = mysql_fetch_array($listSubCategory)){
			?>
        	<option value="<?php echo $subCategory['SubCategoryID'] ?>"><?php echo $subCategory['SubCategoryName'] ?></option>
            <?php
				}
			}
			?>
        </select>
    </td>
    </tr>
    <tr>
    <td>Product Name<span id="require">*</span></td>
    <td>
        <select name="cbProductName" id="cbProductName">
        	<?php
			$listproduct = getAllProduct();
			echo '<option value="0">--Choose--</option>';
			if(mysql_num_rows($listproduct)==0){
				echo '<option value="0">Empty</option>';
			}else{
			while($product = mysql_fetch_array($listproduct)){
			?>
        	<option value="<?php echo $product['ProductID'] ?>"><?php echo $product['ProductName'] ?></option>
            <?php
				}
			}
			?>
        </select>
    </td>
    </tr>
    <tr>
    <td>Product Number<span id="require">*</span></td>
    <td>
        <input id="txtProductNumber" name="txtProductNumber" type="text" value="0">
    </td>
  </tr>
  <tr>
    <td colspan="2"><input id="btnAddProduct" name="btnAddProduct" type="button" value="Add Product"></td>
  </tr>
  <tr id="orderinfo">
    <td>Product Number Total:<label>  0</label><input name="hfProductNumberTotal" type="hidden" value="0"></td>
    <td>Order Amount Total:<label>  0 VND</label><input name="hfOrderAmountTotal" type="hidden" value="0"><input id="hfIntelligentSuggestion" name="hfIntelligentSuggestion" type="hidden" value="0"><input id="hfSuggestionCategoryID" name="hfSuggestionCategoryID" type="hidden" value="0"><input id="hfSuggestionSubCategoryID" name="hfSuggestionSubCategoryID" type="hidden" value="0"></td>
  </tr>
</table>
    
    <input id="btnAddOrder" name="btnAddOrder" type="submit" value="Save Order">
</form>

</div>

<!--footer-->
<?php
require "footer.php";
?>

</body>
</html>